package studio7.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import studio7.dao.SalaDAO;
import studio7.exception.DAOException;
import studio7.modelo.Local;
import studio7.modelo.Sala;
import studio7.negocio.AdministradorNegocio;
import studio7.dao.ServicioDAO;
import studio7.dao.InstrumentoDAO;;

/**
 * Servlet implementation class ReservarServlet
 */
@WebServlet("/ReservarServlet")
public class ReservarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ServicioDAO servDAO = new ServicioDAO();
		InstrumentoDAO instDAO = new InstrumentoDAO();
		
		PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");

        String fecha = request.getParameter("fecha");
        String hora = request.getParameter("hora");
        String horafin = request.getParameter("horafin");
        String capacidad = request.getParameter("capacidadmax");
        String costo = request.getParameter("costototal");
        double costoTotal = Double.parseDouble(costo);
        String sala = request.getParameter("sala");
        String cantServ = request.getParameter("cantservicio");
        String cantInst = request.getParameter("cantinstrumento");
        
        
       
        AdministradorNegocio admin = new AdministradorNegocio();
        SalaDAO salaB = new SalaDAO();
        int idSala = 0;
		try {
			idSala = salaB.BuscarSala(sala);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //insertar Reserva
        Collection<Integer> codInstrumentos = new ArrayList<Integer>();
        Collection<Integer> codServicios = new ArrayList<Integer>();
        
        //Casteando las cantidades a int
        int cantS = Integer.parseInt(cantServ);
        int cantI = Integer.parseInt(cantInst);
        
       //Agregando los servicios al arreglo idServicios
       for(int a=1; a<=cantS;a++){
    	   String check = request.getParameter("checkserv"+a), serv="";
    	   int idServ=0;
    	   if(check.equals("on")){
    		   serv = request.getParameter("servicio"+a);
    		   try {
				idServ=servDAO.BuscarServicioId(serv);
				codServicios.add(idServ);
			} catch (DAOException e) {
				e.printStackTrace();
			}
    	   }   
       }
     //Agregando los intrumentos al arreglo idInstrumentos
       for(int b=1; b<=cantI;b++){
    	   String check = request.getParameter("checkinst"+b), inst="";
    	   int idInst=0;
    	   
    	   System.out.print("Imprime el checkIns"+check+"   ");
    	   if(check.equals("on")){
    		   inst = request.getParameter("instrumento"+b);
    		   try {
				idInst=instDAO.BuscarInstrumentoID(inst);
				System.out.print("Nombre Instrumento: "+ inst);
				System.out.print("ID Instrumento: "+ idInst);
				codInstrumentos.add(idInst);
			} catch (DAOException e) {
				e.printStackTrace();
			}
    	   }   
       }
       
       //INSERTANDO LAS RESERVAS
       try {
			admin.insertarReserva(fecha, hora, horafin, 1, costoTotal,111, idSala, codInstrumentos, codServicios);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.println("Se registro Salisfactoriamente la sala:" + sala);
        
        //Fin de Insertar reserva
        out.println("</body>");
        out.println("</html>");
        out.close();
        
	}

}
